﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace TfL
{
    //[Serializable]
    [XmlType("T")]
    public class Train
    {
        public Train()
        {
            Set = 0;
            Trip = 0;
            Destination = string.Empty;
            DestinationCode = 0;
            TimeToStation = TimeSpan.FromSeconds(0);
            Location = string.Empty;
        }
        [XmlAttribute("S")]
        public int Set { get;  set; }

        [XmlAttribute("T")]
        public int Trip { get;  set; }

        [XmlAttribute("D")]
        public int DestinationCode{ get;  set; }

        [XmlAttribute("DE")]
        public string Destination { get;  set; }

        [XmlAttribute("C")]
        public TimeSpan TimeToStation { get; set; }

        [XmlAttribute("L")]
        public string Location { get; set; }

        public void Parse(XmlReader reader)
        {
            TimeSpan timeSpan;
            if(TimeSpan.TryParseExact(reader.GetAttribute("C"), @"mm\:ss", CultureInfo.CurrentCulture,out timeSpan))
            {
                TimeToStation = timeSpan;
            }
            Destination = reader.GetAttribute("DE");
            DestinationCode = int.Parse(reader.GetAttribute("D"));
            Location = reader.GetAttribute("L");
            Set = int.Parse(reader.GetAttribute("S"));
            Trip = int.Parse(reader.GetAttribute("T"));
            
        }

        public override string ToString()
        {
            return string.Format("{0}: currently {1}: {2}s", Destination, Location,TimeToStation.TotalSeconds);
        }
    }
}
